package com.mes.bac.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mes.bac.backend.common.vo.R;
import com.mes.bac.backend.entity.Product;
import com.mes.bac.backend.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "1. 产品管理", description = "用于管理产品基础信息")
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Operation(summary = "新增或更新产品")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody Product product) {
        // 对于更新操作，确保ID不为空
        return R.success(productService.saveOrUpdate(product));
    }

    @Operation(summary = "根据ID删除产品")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@Parameter(description = "产品ID") @PathVariable String id) {
        return R.success(productService.removeById(id));
    }

    @Operation(summary = "根据ID查询产品详情")
    @GetMapping("/get/{id}")
    public R<Product> getById(@Parameter(description = "产品ID") @PathVariable String id) {
        return R.success(productService.getById(id));
    }

    @Operation(summary = "查询所有产品列表")
    @GetMapping("/list")
    public R<List<Product>> list() {
        return R.success(productService.list());
    }

    @Operation(summary = "分页查询产品")
    @GetMapping("/page")
    public R<IPage<Product>> page(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") long size
    ) {
        Page<Product> page = new Page<>(current, size);
        return R.success(productService.page(page));
    }
}